package com.example.innovate.controller;

/*
    2021.9.13 update/
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.example.innovate.bean.Result;
import com.example.innovate.domain.AdminInfoDO;
import com.example.innovate.domain.CreatedClassDO;
import com.example.innovate.service.AdminService;
import com.example.innovate.service.CreatedClassService;
import com.example.innovate.utils.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/api/createdClass")
@Api(tags = "双创班信息接口")
public class CreatedClassController {
    @Autowired
    CreatedClassService createdClassService;
    @Autowired
    AdminService adminService;

    //@UserLoginToken
    @GetMapping("/findAllCreatedClass")
    @ApiOperation("查找所有双创班信息")
    public Result findAllCreatedClass(@RequestParam("pageNumber") Integer pageNumber,
                                      @RequestParam("pageSize") Integer pageSize,
                                      @RequestParam("className") String className) throws Exception{

        if(className.equals("superAdmin")){
            List<CreatedClassDO> list = createdClassService.findAll(pageNumber,pageSize);
            return new Result(200,"success","返回成功",list);
        }
        else{
            List<CreatedClassDO> list = createdClassService.findAllByClassName(className,pageNumber,pageSize);
            return new Result(200,"success","返回成功",list);
        }
    }

    //@UserLoginToken
    @PostMapping("/upload")
    @ApiOperation("上传")
    public Result upload(@RequestParam("file") MultipartFile file, @RequestParam("id") int id, @RequestParam("className") String className) throws Exception{
        String fileName = FileUtil.upload(file);
        CreatedClassDO createdClassDO = createdClassService.findCreatedClassById(id);
        String filePath = "/root/file/";
        File file1 = new File(filePath + fileName);
        // 如果文件存在
        String s = "";
        if (file1.exists()) {
            s = "http://42.193.219.136:8080/images/"+fileName;
        }
        else {
            s = "文件上传失败";
        }
        if(createdClassDO==null){
            CreatedClassDO createdClassDO1 = new CreatedClassDO();
            createdClassDO1.setClassIntroduceImage(s);
            createdClassDO1.setClassName(className);
            createdClassService.addCreatedClass(createdClassDO1);
            return new Result(200,"success","添加成功",createdClassDO1);
        }
        else{
            if(createdClassDO.getClassIntroduceImage()!=null){
                String deleteName = createdClassDO.getClassIntroduceImage();
                filePath = "/root/file/" ;//C:\\file\\
                File filePast = new File(filePath + deleteName);
                if (filePast.isFile() && filePast.exists()) {
                    filePast.delete();
                }
            }
            createdClassDO.setClassIntroduceImage(s);
            createdClassService.updateCreatedClass(createdClassDO);
            return new Result(200,"success","添加成功",createdClassDO);
        }
    }

    //@UserLoginToken
    @PostMapping("/addCreatedClass")
    @ApiOperation("添加双创班信息")
    public Result addCreatedClass(@RequestBody CreatedClassDO createdClassDO) throws Exception{
        CreatedClassDO createdClassDO1 = createdClassService.findByImage(createdClassDO.getClassIntroduceImage());
        if(createdClassDO1==null){
            createdClassService.addCreatedClass(createdClassDO);
            return new Result(200,"success","添加成功",createdClassDO);
        }
        else{
            createdClassDO.setClassId(createdClassDO1.getClassId());
            createdClassDO.setClassIntroduceImage(createdClassDO1.getClassIntroduceImage());
            createdClassService.updateCreatedClass(createdClassDO);
            return new Result(200,"success","添加成功",createdClassDO);
        }
    }

    //@UserLoginToken
    @PostMapping("/updateCreatedClass")
    @ApiOperation("修改双创班信息")
    public Result updateCreatedClass(@RequestBody CreatedClassDO createdClassDO,
                                     @RequestParam("adminUserName") String adminUserName) throws Exception{
        AdminInfoDO adminInfoDO = adminService.getAdminInfoByName(adminUserName);
        if(adminInfoDO!=null){
            if(adminInfoDO.getAdminIsUpdate().equals("false")){
                return new Result(400,"error","没有修改权限",null);
            }
            else{
                createdClassService.updateCreatedClass(createdClassDO);
                return new Result(200,"success","修改成功",createdClassDO);
            }
        }
        else{
            createdClassService.updateCreatedClass(createdClassDO);
            return new Result(200,"success","修改成功",createdClassDO);
        }
    }

    //@UserLoginToken
    @PostMapping("/deleteCreatedClass")
    @ApiOperation("删除双创班信息")
    public Result deleteCreatedClass(@RequestParam("id") int id,
                                     @RequestParam("adminUserName") String adminUserName) throws Exception{
        AdminInfoDO adminInfoDO = adminService.getAdminInfoByName(adminUserName);
        if(adminInfoDO!=null){
            if(adminInfoDO.getAdminIsUpdate().equals("false")){
                return new Result(400,"error","没有删除权限",null);
            }
            else{
                createdClassService.deleteCreatedClass(id);
                return new Result(200,"success","删除成功","id= "+id);
            }
        }
        else{
            return new Result(400,"error","管理员用户名不存在","adminUserName= "+adminUserName);
        }


    }

//    @UserLoginToken
//    @ApiOperation("双创班信息Excel导入")
//    @PostMapping(value = "/import")
//    public Result importExcel(@RequestParam("file") MultipartFile file, @RequestParam("className") String className) throws IOException {
//        List<CreatedClass> list = ExcelUtils.importExcel(file,1,1,CreatedClass.class);
//        CreatedClass createdClass = new CreatedClass();
//        for(int i = 0 ; i < list.size();i++){
//            createdClass = list.get(i);
//            createdClass.setId(0);
//            createdClassService.addCreatedClass(createdClass);
//        }
//        return new Result(200,"success","导入成功",list);
//    }
//
//    @UserLoginToken
//    @ApiOperation("双创班信息Excel导出")
//    @GetMapping("/export")
//    public Result export(HttpServletResponse response, @RequestParam("className") String className) throws IOException {
//        if(className.equals("superAdmin")){
//            List<CreatedClass> createdClasses = createdClassService.findAll();
//            for(int i=0;i<createdClasses.size();i++){
//                createdClasses.get(i).setId(i+1);
//            }
//            ExcelUtils.exportExcel(createdClasses,"双创班信息","sheet1",CreatedClass.class,"双创班信息",response);
//            return new Result(200,"success","导出成功",createdClasses);
//        }
//        else{
//            List<CreatedClass> createdClasses = createdClassService.findAllByClass(className);
//            for(int i=0;i<createdClasses.size();i++){
//                createdClasses.get(i).setId(i+1);
//            }
//            ExcelUtils.exportExcel(createdClasses,className+"双创班信息","sheet1",CreatedClass.class,className+"双创班信息",response);
//            return new Result(200,"success","导出成功",createdClasses);
//        }
//    }

    //@UserLoginToken
    @GetMapping("/findCreatedClass")
    @ApiOperation("查找双创班信息")
    public Result findCreatedClass(@RequestParam("pageNumber") Integer pageNumber,
                                   @RequestParam("pageSize") Integer pageSize,
                                   @RequestParam("startTime") String startTime,
                                   @RequestParam("endTime") String endTime,
                                   @RequestParam("className") String className) throws Exception{
        if (pageNumber == null){
            pageNumber = 1;
        }
        if(className.equals("superAdmin")){
            className = null;
        }
        List<CreatedClassDO> createdClassDOS = createdClassService.findCreatedClassPage(pageNumber,pageSize,className,startTime,endTime);
        if(!createdClassDOS.isEmpty()){
            return new Result(200,"success","查找成功",createdClassDOS);
        }
        else{
            return new Result(400,"error","所查找信息不存在",null);
        }
    }

//    @UserLoginToken
//    @ApiOperation("查找信息Excel导出")
//    @GetMapping("/findExport")
//    public Result createdClassExport(HttpServletResponse response,
//                                    @RequestParam("baseName") String baseName,
//                                    @RequestParam("foundCollege") String foundCollege,
//                                    @RequestParam("startTime") String startTime,
//                                    @RequestParam("endTime") String endTime,
//                                    @RequestParam("className") String className) throws IOException {
//        if(className.equals("superAdmin")){
//            className = null;
//            List<CreatedClass> createdClasses = createdClassService.findCreatedClass(baseName,foundCollege,className,startTime,endTime);
//            for(int i=0;i<createdClasses.size();i++){
//                createdClasses.get(i).setId(i+1);
//            }
//            ExcelUtils.exportExcel(createdClasses,"双创班信息","sheet1",CreatedClass.class,"双创班合作信息",response);
//            return new Result(200,"success","导出成功",createdClasses);
//        }
//        else{
//            List<CreatedClass> createdClasses = createdClassService.findCreatedClass(baseName,foundCollege,className,startTime,endTime);
//            for(int i=0;i<createdClasses.size();i++){
//                createdClasses.get(i).setId(i+1);
//            }
//            ExcelUtils.exportExcel(createdClasses,className+"双创班信息","sheet1",CreatedClass.class,className+"双创班合作信息",response);
//            return new Result(200,"success","导出成功",createdClasses);
//        }
//    }
}
